import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import '../model/cartInfo.dart';

class CartProvide with ChangeNotifier{
  String cartString = "[]";
  List<CartInfoModel> cartList=[];
  double allPrice=0;
  int allGoodsCount=0;
  bool isAllCheck=true;

  save(goodsId,goodsName,count,price,images) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    var temp =cartString==null?[]:json.decode(cartString.toString());
    List<Map> tempList = (temp as List).cast();
    bool isHave = false;
    int ival = 0;
    allPrice=0;
    allGoodsCount=0;
    tempList.forEach((item){
      if(item['goodsId']==goodsId){
        tempList[ival]['count']=item['count']+1;
        cartList[ival].count++;
        isHave = true;
      }
      if(item['isCheck']){
        allPrice+=(cartList[ival].price*cartList[ival].count);
        allGoodsCount+=cartList[ival].count;
      }
      ival++;
    });
    if(!isHave){
      Map<String,dynamic> newGoods = {
        'goodsId': goodsId,
        'goodsName': goodsName,
        'count': count,
        'price': price,
        'images': images,
        'isCheck': true,
      };
      tempList.add(newGoods);
      cartList.add(CartInfoModel.fromJson(newGoods));
      allPrice=count*price;
      allGoodsCount+=count;
    }
    cartString=json.encode(tempList).toString();
    prefs.setString('cartInfo', cartString);
    notifyListeners();
  }

  remove() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove('cartInfo');
    cartList=[];
    print('清空完成');
    notifyListeners();
  }

  getCardInfo()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    cartList=[];
    if(cartString==null){
      cartList=[];
      allPrice=0;
      allGoodsCount=0;
      isAllCheck=true;
    } else {
      List<Map> tempList = (json.decode(cartString.toString()) as List).cast();
      allPrice=0;
      allGoodsCount=0;
      isAllCheck=true;
      tempList.forEach((item){
        cartList.add(CartInfoModel.fromJson(item));
        if(item['isCheck']){
           allPrice+=item['count']*item['price'];
           allGoodsCount+=item['count'];
        } else {
          isAllCheck=false;
        }
      });
    }
    notifyListeners();
  }

  //删除单个购物车商品
  deleteOneGoods(String goodsId) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    List<Map> tempList = (json.decode(cartString.toString()) as List).cast();
    int tempIndex=0;
    int deleteIndx=0;
    tempList.forEach((item){
      if(item['goodsId']==goodsId){
        deleteIndx=tempIndex;
      }
      tempIndex++;
    });
    tempList.removeAt(deleteIndx);
    cartString=json.encode(tempList).toString();
    prefs.setString('cartInfo', cartString);
    getCardInfo();
  }

  //修改单个商品选中状态
  changeCheckState(CartInfoModel cartItem) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    List<Map> tempList = (json.decode(cartString.toString()) as List).cast();
    int tempIndex=0;
    int changeIndex=0;
    tempList.forEach((item){
      if(item['goodsId']==cartItem.goodsId){
        changeIndex=tempIndex;
      }
      tempIndex++;
    });
    tempList[changeIndex]=cartItem.toJson();
    cartString=json.encode(tempList).toString();
    prefs.setString('cartInfo', cartString);
    getCardInfo();
  }

  //修改全选按钮状态
  changeAllCheckBtnState(bool isCheck) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    List<Map> tempList = (json.decode(cartString.toString()) as List).cast();
    List<Map> newList=[];
    for(var item in tempList){
      var newItem = item;
      newItem['isCheck']=isCheck;
      newList.add(newItem);
    }
    cartString=json.encode(newList).toString();
    prefs.setString('cartInfo', cartString);
    getCardInfo();
  }

  //商品数量加减
  addOrReduceAction(var cartItem, String todo) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    cartString = prefs.getString('cartInfo');
    List<Map> tempList = (json.decode(cartString.toString()) as List).cast();
    int tempIndex=0;
    int changeIndex=0;
    for(var item in tempList){
      if(item['goodsId'] == cartItem.goodsId){
         changeIndex=tempIndex;
      }
      tempIndex++;
    }
    if (todo=='add') {
      cartItem.count++;
    } else if(cartItem.count>1) {
      cartItem.count--;
    }
    tempList[changeIndex]=cartItem.toJson();
    cartString=json.encode(tempList).toString();
    prefs.setString('cartInfo', cartString);
    getCardInfo();

  }
}